package fantasyday.wowzolo.web.account;

import fantasyday.wowzolo.domain.Account;
import fantasyday.wowzolo.service.AccountService;
import fantasyday.wowzolo.web.catalog.IndexPage;
import fantasyday.wowzolo.web.common.BasePage;
import fantasyday.wowzolo.web.common.ErrorMessage;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SignonPage extends BasePage {
	@SpringBean
	private AccountService accountService;
	
	@SuppressWarnings("serial")
	public SignonPage() {
		Account account = new Account();
		account.setUsername("j2ee");
		account.setPassword("j2ee");
		
		Form form = new Form("signonForm", new CompoundPropertyModel(account)) {
			@Override
			protected void onSubmit() {
				Account account = (Account)getModelObject();
				Account signonAccount = accountService.getAccount(account);
				
				if (signonAccount != null) {
					getJpetstoreSession().setSignonedAccount(signonAccount);
					setResponsePage(IndexPage.class);
				} else {
					getSession().error(ErrorMessage.signonFail);
				}
			}
		};
		 
		RequiredTextField nameField = new RequiredTextField("username");
		
		PasswordTextField passField = new PasswordTextField("password");
		passField.setResetPassword(false);
		
		form.add(nameField);
		form.add(passField);
		
		add(form);
		
		add(new BookmarkablePageLink("registerLink", RegisterPage.class));
	}
}
